#include "Sphere.h"
#include "Common.h"
#include "Eitoboru.h"


Sphere::Sphere( scene::ISceneNode *node )
{
    m_Node = node;
    m_Velocity = core::vector3df( 0.0f, 0.0f, 0.0f );
}


Sphere::~Sphere( void )
{
    m_Node->remove();
}


void Sphere::Update( void )
{
    core::vector3df pos = m_Node->getPosition();

    // Apply previous velocity
    pos += m_Velocity;

    // Set new position
    m_Node->setPosition( pos );
    
    core::vector3d<irr::f32> edges[8];
    g_TableSurfaceBox->getTransformedBoundingBox().getEdges( edges );

    // Unless we're going to leave the scene, apply gravity
    if( (pos.Y + m_Velocity.Y) > edges[0].Y )
    {
        m_Velocity.Y -= GRAVITY;
    }
    else
    {
        m_Velocity.Y = 0.0f;
        m_Velocity *= 0.99f;
    }

    // Check leaving scene at x axis
    if( m_Velocity.X != 0.0f )
    {
        if( (pos.X + m_Velocity.X) <= edges[0].X )
        {
            m_Velocity.X = -m_Velocity.X;
            m_Velocity *= 0.7f;
        }
        else if( (pos.X + m_Velocity.X) >= edges[6].X )
        {
            m_Velocity.X = -m_Velocity.X;
            m_Velocity *= 0.7f;
        }
    }

    // Check leaving scene at z axis
    if( m_Velocity.Z != 0.0f )
    {
        if( (pos.Z + m_Velocity.Z) <= edges[0].Z )
        {
            m_Velocity.Z = -m_Velocity.Z;
            m_Velocity *= 0.7f;
        }
        else if( (pos.Z + m_Velocity.Z) >= edges[6].Z )
        {
            m_Velocity.Z = -m_Velocity.Z;
            m_Velocity *= 0.7f;
        }
    }
}